﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using BenefitHelper.Sys;
namespace BenefitHelper.Data.Day
{
    /// <summary>
    /// 账户资金变动(对于个人多账号后显示的总计值在while循环后重新写一个对象写入)这个是显示在第一行的总数(u)
    /// </summary>
    public class AccountDayChange
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Account Account { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public int TradeHistoryId { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public virtual TradeHistory TradeHistory { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }




        /// <summary>
        /// 删除当日所有记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId, DB.DBManager db)
        {
            var query = from t in db.AccountDayChange where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Data.Day.AccountDayChange c in query)
            {
                db.AccountDayChange.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存账号日变化情况
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitDayAccountDayChange(int tradeHistoryId, string pdate, DB.DBManager db)
        {
            List<Data.ServerData.T_Account> serverAccounts = new List<ServerData.T_Account>();
            int connectionStringCount = System.Configuration.ConfigurationManager.ConnectionStrings.Count;
            for (int i = 0; i < connectionStringCount; i++)
            {
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[i].ConnectionString;
                if (System.Configuration.ConfigurationManager.ConnectionStrings[i].ProviderName.Equals("System.Data.OracleClient"))
                {
                    Common.ADO.Tools.OracleHelper.connectionString = connectionString;
                    List<BenefitHelper.Data.ServerData.T_Account> accountList = new BenefitHelper.Data.ServerData.T_Account().GetList();
                    foreach (BenefitHelper.Data.ServerData.T_Account _account in accountList)
                    {
                        serverAccounts.Add(_account);
                    }
                }
            }
            System.Configuration.ConnectionStringSettingsCollection colls = System.Configuration.ConfigurationManager.ConnectionStrings;

            foreach (Data.ServerData.T_Account account in serverAccounts)
            {
                ("发现新的账户：" + account.LoginAccount).Debug();
                Data.Day.AccountDayChange change = new Data.Day.AccountDayChange();
                Sys.Account _account = new Sys.Account();
                change.AccountId = _account.GetAccountId(account.LoginAccount);
                Data.ServerData.T_TradeDetail_History tth = new Data.ServerData.T_TradeDetail_History();
                change.BillCount = tth.GetDayBillCount(pdate, account.Id, db);
                if (change.BillCount > 0)
                {
                    Data.ServerData.T_SettlementMoney money = new ServerData.T_SettlementMoney();
                    money = money.GetSettlementMoney(account.Id, pdate, db);
                    change.DayCount = money.CloseProfit - money.Commission;
                    change.Free = money.Commission;
                    change.LoseCount = tth.GetDayLoseCount(pdate, account.Id, db);
                    change.Profit = money.CloseProfit;
                    change.SumLost = tth.GetDayAllLose(pdate, account.Id, db);
                    change.SumWin = tth.GetDayAllWin(pdate, account.Id, db);
                    change.TradeHistoryId = tradeHistoryId;
                    change.UsedMargin = money.Margin;
                    change.WinCount = tth.GetDayWinCount(pdate, account.Id, db);
                    db.AccountDayChange.Add(change);
                }
            }
            db.SaveChanges();
        }

        public AccountDayChange GetAccountDayChange(int tradeHistoryId, int accountid, DB.DBManager db)
        {
            AccountDayChange returnValue = new AccountDayChange();
            var query = db.AccountDayChange.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                returnValue = query.First();
            }
            return returnValue;
        }
    }
}